package thread;


import java.io.*;

public class ComRunCpp extends ComRunThread.ComRun {
    private String sourceName;
    private String result;
    public ComRunCpp(String sourceName){
        this.sourceName=sourceName;
    }

    public String getSourceName() {
        return sourceName;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    public String getResult() {
        return result;
    }

    @Override
    public void run() {
        try{
            //Thread.sleep(1000);
            Runtime runtime = Runtime.getRuntime();
            String[] commend={"cmd","/c","g++","-o", sourceName+".exe",sourceName+".cpp","&&",sourceName+".exe","<",sourceName+".txt"};
            Process pr = runtime.exec(commend, null, new File("D:\\onlineDemo"));
            SequenceInputStream sis = new SequenceInputStream(pr.getErrorStream(), pr.getInputStream());
            InputStreamReader isr = new InputStreamReader(sis, "GBK");
            BufferedReader br = new BufferedReader(isr);
            StringBuilder strb = new StringBuilder();
            String res=null;
            while((res=br.readLine())!=null){
                strb.append(res+"\n");
            }
            br.close();
            pr.waitFor();
            pr.destroy();
            if(strb.equals(""))
                result="操作完成";
            else
                result=strb.toString();
        }catch (InterruptedException e) {
            result="运行超时，已打断，请重试！";
            return;
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
